/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author duongna
 */
public class Status {
    private int writerId;
    private Account writerAcc;
    private String content;
    private Date date;

    public int getWriterId() {
        return writerId;
    }

    public void setWriterId(int writerId) {
        this.writerId = writerId;
    }

    public Account getWriterAcc() {
        return writerAcc;
    }

    public void setWriterAcc(Account writerAcc) {
        this.writerAcc = writerAcc;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * @return the amount of time since the status is posted
     */
    public String getTimeAgo() {
        long time = ((new Date()).getTime() - date.getTime())/1000; // in unit: second
        
        int nDays =  (int) time/(60*60*24);
        int nHours = (int) (time - nDays*60*60*24)/(60*60);
        int nMins = (int) (time - nDays*60*60*24 - nHours*60*60)/(60);
        int nSecs = (int) (time - nDays*60*60*24 - nHours*60*60 - nMins*60);
        if (nDays > 0) {
            // display full date form
            StringBuilder builder = new StringBuilder();
            SimpleDateFormat formater = new SimpleDateFormat("MMMM dd");
            builder.append(formater.format(date));
            builder.append(" at ");
            
            formater = new SimpleDateFormat("kk:mm");
            builder.append(formater.format(date));
            return builder.toString();
        } else if (nHours > 0) {
            if (nHours == 1) {
                return ("about an hour ago.");
            } else {
                return (nHours + " hours ago.");
            }
        } else if (nMins > 0) {
            if (nMins == 1) {
                return ("about an minute ago.");
            } else {
                return (nMins + " minutes ago.");
            }
        } else {
            return "a few seconds ago.";
        }
    }
    
    /**
     * set basic info for status from result set
     */
    public void setBasicInfo(ResultSet resultSet) throws SQLException {
        setWriterId(resultSet.getInt("writerId"));
        setContent(resultSet.getString("content"));
        setDate(resultSet.getTimestamp("date"));
    }
}
